package zjc.bookkeeping.PhotoRecognition;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import zjc.bookkeeping.R;
import zjc.bookkeeping.activity.MainActivity;
import zjc.bookkeeping.db.ConsumptionBean;
import zjc.bookkeeping.db.ConsumptionClassBean;
import zjc.bookkeeping.db.DBManager;
import zjc.bookkeeping.db.MethodBean;
import zjc.bookkeeping.minepage.bClassificationkeywords.LabelActivity;
import zjc.bookkeeping.tools.TimeDialog;

public class addPhotoConsumptionActivity extends AppCompatActivity {
    private ConsumptionBean consumptionBean;

    private String apMethod;
    private String apClassification;
    private EditText apRemarks;
    private EditText apAmount;
    private TextView apTime;
    private EditText apBusinessman;
    private TextView apyear;
    private TextView apmonth;
    private TextView apday;

    int id;

    private Spinner Classification;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_photo_consumption);

//        apMethod = findViewById(R.id.apMethod);
//        apClassification = findViewById(R.id.apClassification);
        apRemarks = findViewById(R.id.apRemarks);
        apAmount = findViewById(R.id.apAmount);
        apTime = findViewById(R.id.apTime);
        apBusinessman = findViewById(R.id.apBusinessman);
        apyear = findViewById(R.id.apyear);
        apmonth = findViewById(R.id.apmonth);
        apday = findViewById(R.id.apday);

        Classification=findViewById(R.id.apClassification);

        initSpinnermethod();
        initSpinner();
        try {
            Modifydetail();
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    public void back(View v){
        Intent intent = new Intent(this, PhotoRecognitionActivity.class);
        startActivity(intent);
    }
    //支付方式
    //查询数据
    private String[] methodname(){
        List<MethodBean> list = DBManager.getAllMethod();
        int k = 0 ;
        String[] text = new String[list.size()];
        k = list.size();
        Log.i("1.k", String.valueOf(k));
        for (int i = 0;i<k;i++) {
            String name = list.get(i).getMethod_name();
            text[i] = name;
            System.out.println(i + "账户:" + list.get(i).getMethod_name());
        }
        return text;
    }
    private int[] methodid(){
        List<MethodBean> list = DBManager.getAllMethod();
        int k = 0 ;
        int[] textid = new int[list.size()];
        k = list.size();
        Log.i("1.k", String.valueOf(k));
        for (int i = 0;i<k;i++) {
            textid[i] = list.get(i).getMethod_id();
            System.out.println(i + "账户:" + list.get(i).getMethod_id());
        }
        return textid;
    }

    //下拉框
    private void initSpinnermethod(){
        String[] methodArray = methodname();
        //声明一个下拉列表的数组适配器
        ArrayAdapter<String> starAdapter = new ArrayAdapter<String>(this,R.layout.b_item_select,methodArray);
        //设置数组适配器的布局样式
        starAdapter.setDropDownViewResource(R.layout.b_item_dropdown);
        //从布局文件中获取名叫sp_dialog的下拉框
        Spinner sp = findViewById(R.id.apMethod);
        //设置下拉框的标题，不设置就没有难看的标题了
        sp.setPrompt("请选择分类");
        //设置下拉框的数组适配器
        sp.setAdapter(starAdapter);
        //设置下拉框默认的显示第一项
        sp.setSelection(0);
        //给下拉框设置选择监听器，一旦用户选中某一项，就触发监听器的onItemSelected方法
        sp.setOnItemSelectedListener(new MySelectedListenerMethond());
    }
    class MySelectedListenerMethond implements AdapterView.OnItemSelectedListener{
        String[] methodArray = methodname();
        int[] idArray=methodid();
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            Toast.makeText(addPhotoConsumptionActivity.this,"您选择的是："+methodArray[i],Toast.LENGTH_SHORT).show();
            apMethod=methodArray[i];
            id=idArray[i];
        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

        }
    }

//分类
    //查询数据
    private String[] classname(int j){
        List<ConsumptionClassBean> list = DBManager.getConsumptionClass(j);
        int k = 0 ;
        String[] text = new String[list.size()];
        k = list.size();
        Log.i("1.k", String.valueOf(k));
        for (int i = 0;i<k;i++) {
            String name = list.get(i).getConsumption_class_name();
            text[i] = name;
            System.out.println(i + "账户:" + list.get(i).getConsumption_class_kind() + list.get(i).getConsumption_class_name() + list.get(i).getConsumption_class_image());
        }
        return text;
    }

    //下拉框
    private void initSpinner(){
        String[] classArray1 = classname(0);
        //声明一个下拉列表的数组适配器
        ArrayAdapter<String> starAdapter = new ArrayAdapter<String>(this,R.layout.b_item_select,classArray1);
        //设置数组适配器的布局样式
        starAdapter.setDropDownViewResource(R.layout.b_item_dropdown);
        //从布局文件中获取名叫sp_dialog的下拉框
        //Spinner sp = findViewById(R.id.apClassification);
        //设置下拉框的标题，不设置就没有难看的标题了
        Classification.setPrompt("请选择分类");
        //设置下拉框的数组适配器
        Classification.setAdapter(starAdapter);
        //设置下拉框默认的显示第一项
        //sp.setSelection(0);
        //给下拉框设置选择监听器，一旦用户选中某一项，就触发监听器的onItemSelected方法
        Classification.setOnItemSelectedListener(new MySelectedListener());
    }
    class MySelectedListener implements AdapterView.OnItemSelectedListener{
        String[] classArray1 = classname(0);
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            Toast.makeText(addPhotoConsumptionActivity.this,"您选择的是："+classArray1[i],Toast.LENGTH_SHORT).show();
            apClassification=classArray1[i];
        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

        }
    }
    //打开选择时间
    public void openSelectTime(View v){
        showTimeDialog();
    }
    private void showTimeDialog() {
        TimeDialog timeDialog = new TimeDialog(this);
        timeDialog.show();
        timeDialog.setOnTimeSureListener(new TimeDialog.OnTimeSureListener() {
            @Override
            public void onTimeSure(String time, int year, int month, int day) {
                apTime.setText(time);

                String time1=apTime.getText().toString();
                String[] timei = new String[5];
                String numStr = "";
                int k=0;
                for (int n = 0; n < time1.length(); n++) {
                    String subStr = time1.substring(n, n+1);
                    Pattern pattern = Pattern.compile("[0-9]+");
                    Matcher matcher = pattern.matcher(subStr);
                    if (matcher.find()) {
                        numStr +=subStr;
                    }else {
                        timei[k]=numStr;
                        k++;
                        numStr="";
                    }
                }
                apyear.setText(timei[0]);
                apmonth.setText(timei[1]);
                apday.setText(timei[2]);
            }
        });
    }
    //拍照传值
    private void Modifydetail() throws JSONException {
        //获取传递的字符串值
        Intent intent =getIntent();
        String exvalues = intent.getStringExtra("values");
        String exbilltype = intent.getStringExtra("billtype");
        System.out.println(exvalues);

        JSONObject jsonObject = new JSONObject(exvalues);
        String item_list = jsonObject.getJSONObject("result").getString("item_list");
        Log.i("item_list",item_list);

        JSONArray jsonarray = new JSONArray(item_list);
        switch (exbilltype){
            case "ShopTicket":
                try{
                    Log.i("jsonarray", String.valueOf(jsonarray));
                    Classification.setSelection(3);
                    for(int i = 0 ;i<jsonarray.length();i++)
                    {
                        //通过JSONObject类把JSON字符串转换为java对象，以便于后续操作
                        JSONObject jsonobject = jsonarray.getJSONObject(i);
                        String description = jsonobject.optString("description");
                        String value = jsonobject.optString("value");
                        if (description.equals("金额")){
                            String numStr="";
                            for (int n = 0; n < value.length(); n++) {
                                String subStr = value.substring(n, n+1);
                                Pattern pattern = Pattern.compile("[\\d.]");
                                Matcher matcher = pattern.matcher(subStr);
                                if (matcher.find()) {
                                    numStr +=subStr;
                                }
                            }
                            if(numStr==""){
                                Toast.makeText(getApplicationContext(), "金额为空,请添加金额", Toast.LENGTH_LONG).show();
                            }else{
                                apAmount.setText(numStr);
                            }
                            Log.i("金额",numStr);
                        }else if(description.equals("时间")){
                            Log.i("时间",value);
                            String[] timei = new String[5];
                            String numStr = "";
                            int k=0;
                            for (int n = 0; n < value.length(); n++) {
                                String subStr = value.substring(n, n+1);
                                Pattern pattern = Pattern.compile("[0-9]+");
                                Matcher matcher = pattern.matcher(subStr);
                                if (matcher.find()) {
                                    numStr +=subStr;
                                }else {
                                    timei[k]=numStr;
                                    k++;
                                    numStr="";
                                }
                            }
                            String time= timei[0]+"年"+timei[1]+"月"+timei[2]+"日 "+timei[3]+":"+timei[4] ;
                            Log.i("time",time);
                            apTime.setText(time);
                            apyear.setText(timei[0]);
                            apmonth.setText(timei[1]);
                            apday.setText(timei[2]);

                            if(time.contains("null")){
                                Log.i("时间不对","不对");
                                Date date = new Date();
                                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
                                String time1 = simpleDateFormat.format(date);
                                apTime.setText(time1);
                                Calendar calendar = Calendar.getInstance();
                                String year = String.valueOf(calendar.get(Calendar.YEAR));
                                String month = String.valueOf(calendar.get(Calendar.MONTH) + 1);
                                String day = String.valueOf(calendar.get(Calendar.DAY_OF_MONTH));
                                apyear.setText(year);
                                apmonth.setText(month);
                                apday.setText(day);
                            }
                        }else if(description.equals("商户")){
                            apBusinessman.setText(value);
                            Log.i("商户",value);
                        }else if(description.equals("商品")){
                            apRemarks.setText(value);
                            Log.i("商品",value);
                        }else {
                            Log.i("问题","都不对");
                        }
                    }
                    //抛出错误
                }catch(JSONException e){
                    e.printStackTrace();
                }
                break;
            case "TrainTickets":
                try{
                    Log.i("jsonarray", String.valueOf(jsonarray));
                    Classification.setSelection(1);
                    String s = "";
                    for(int i = 0 ;i<jsonarray.length();i++)
                    {
                        //通过JSONObject类把JSON字符串转换为java对象，以便于后续操作
                        JSONObject jsonobject = jsonarray.getJSONObject(i);
                        String description = jsonobject.optString("description");
                        String value = jsonobject.optString("value");
                        if (description.equals("价格")){
                            String numStr="";
                            for (int n = 0; n < value.length(); n++) {
                                String subStr = value.substring(n, n+1);
                                Pattern pattern = Pattern.compile("[\\d.]");
                                Matcher matcher = pattern.matcher(subStr);
                                if (matcher.find()) {
                                    numStr +=subStr;
                                }
                            }
                            if(numStr==""){
                                Toast.makeText(getApplicationContext(), "金额为空,请添加金额", Toast.LENGTH_LONG).show();
                            }else{
                                apAmount.setText(numStr);
                            }
                            Log.i("金额",numStr);
                        }else if(description.equals("乘车时间")){
                            Log.i("乘车时间",value);
                            String[] timei = new String[5];
                            String numStr = "";
                            int k=0;
                            Log.i("value.length()", String.valueOf(value.length()));
                            for (int n = 0; n < value.length(); n++) {
                                String subStr = value.substring(n, n+1);
                                Pattern pattern = Pattern.compile("[0-9]+");
                                Matcher matcher = pattern.matcher(subStr);
                                if (matcher.find()) {
                                    numStr +=subStr;
                                    Log.i(n+".numStr:",numStr);
                                }else {
                                    timei[k]=numStr;
                                    Log.i("numStr"+k+":",numStr);
                                    k++;
                                    numStr="";
                                }
                                timei[4]=numStr;
                            }
                            String time= timei[0]+"年"+timei[1]+"月"+timei[2]+"日 "+timei[3]+":"+timei[4] ;
                            Log.i("time",time);
                            apTime.setText(time);
                            apyear.setText(timei[0]);
                            apmonth.setText(timei[1]);
                            apday.setText(timei[2]);
                            if(time.contains("null")){
                                Log.i("时间不对","不对");
                                Date date = new Date();
                                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
                                String time1 = simpleDateFormat.format(date);
                                apTime.setText(time1);
                                Calendar calendar = Calendar.getInstance();
                                String year = String.valueOf(calendar.get(Calendar.YEAR));
                                String month = String.valueOf(calendar.get(Calendar.MONTH) + 1);
                                String day = String.valueOf(calendar.get(Calendar.DAY_OF_MONTH));
                                apyear.setText(year);
                                apmonth.setText(month);
                                apday.setText(day);
                            }
                        }else if(description.equals("出发地")){
                            s = value +" - ";
                            Log.i("出发地",value);
                        }else if(description.equals("车次号")){
                            s += value+" - ";
                            Log.i("车次号",value);
                        }else if(description.equals("目的地")){
                            s += value;
                            apRemarks.setText(s);
                            Log.i("目的地",value);
                        }else if(description.equals("检票口")){
                            apBusinessman.setText(value);
                            Log.i("检票口",value);
                        }else {
                            Log.i("问题","都不对");
                        }
                    }

                    //抛出错误
                }catch(JSONException e){
                    e.printStackTrace();
                }
                break;
            case "TaxiInvoice":
                try{
                    Log.i("jsonarray", String.valueOf(jsonarray));
                    Classification.setSelection(1);
                    String s = "";
                    for(int i = 0 ;i<jsonarray.length();i++)
                    {
                        //通过JSONObject类把JSON字符串转换为java对象，以便于后续操作
                        JSONObject jsonobject = jsonarray.getJSONObject(i);
                        String description = jsonobject.optString("description");
                        String value = jsonobject.optString("value");
                        if (description.equals("金额")){
                            String numStr="";
                            for (int n = 0; n < value.length(); n++) {
                                String subStr = value.substring(n, n+1);
                                Pattern pattern = Pattern.compile("[\\d.]");
                                Matcher matcher = pattern.matcher(subStr);
                                if (matcher.find()) {
                                    numStr +=subStr;
                                }
                            }
                            if(numStr==""){
                                Toast.makeText(getApplicationContext(), "金额为空,请添加金额", Toast.LENGTH_LONG).show();
                            }else{
                                apAmount.setText(numStr);
                            }
                            Log.i("金额",numStr);
                        }else if(description.equals("日期")){
                            Log.i("日期",value);
                            String[] timei = new String[5];
                            String numStr = "";
                            int k=0;
                            for (int n = 0; n < value.length(); n++) {
                                String subStr = value.substring(n, n+1);
                                Pattern pattern = Pattern.compile("[0-9]+");
                                Matcher matcher = pattern.matcher(subStr);
                                if (matcher.find()) {
                                    numStr +=subStr;
                                }else {
                                    timei[k]=numStr;
                                    k++;
                                    numStr="";
                                }
                            }
                            String time= timei[0]+"年"+timei[1]+"月"+timei[2]+"日 "+timei[3]+":"+timei[4] ;
                            Log.i("time",time);
                            apTime.setText(time);
                            apyear.setText(timei[0]);
                            apmonth.setText(timei[1]);
                            apday.setText(timei[2]);
                            if(time.contains("null")){
                                Log.i("时间不对","不对");
                                Date date = new Date();
                                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
                                String time1 = simpleDateFormat.format(date);
                                apTime.setText(time1);
                                Calendar calendar = Calendar.getInstance();
                                String year = String.valueOf(calendar.get(Calendar.YEAR));
                                String month = String.valueOf(calendar.get(Calendar.MONTH) + 1);
                                String day = String.valueOf(calendar.get(Calendar.DAY_OF_MONTH));
                                apyear.setText(year);
                                apmonth.setText(month);
                                apday.setText(day);
                            }
                        }else if(description.equals("车号")){
                            s=value+" 里程";
                            apBusinessman.setText(value);
                            Log.i("车号",value);
                        }else if(description.equals("里程")){
                            s+=value;
                            apRemarks.setText(s);
                            Log.i("里程",value);
                        }else {
                            Log.i("问题","都不对");
                        }
                    }

                    //抛出错误
                }catch(JSONException e){
                    e.printStackTrace();
                }
                break;
            case "RoadToll":
                try{
                    Log.i("jsonarray", String.valueOf(jsonarray));
                    Classification.setSelection(1);
                    String[] timei = new String[5];
                    String time = "";
                    String s="";
                    for(int i = 0 ;i<jsonarray.length();i++)
                    {
                        //通过JSONObject类把JSON字符串转换为java对象，以便于后续操作
                        JSONObject jsonobject = jsonarray.getJSONObject(i);
                        String description = jsonobject.optString("description");
                        String value = jsonobject.optString("value");
                        if (description.equals("收费金额")){
                            String numStr="";
                            for (int n = 0; n < value.length(); n++) {
                                String subStr = value.substring(n, n+1);
                                Pattern pattern = Pattern.compile("[\\d.]");
                                Matcher matcher = pattern.matcher(subStr);
                                if (matcher.find()) {
                                    numStr +=subStr;
                                }
                            }
                            if(numStr==""){
                                Toast.makeText(getApplicationContext(), "金额为空,请添加金额", Toast.LENGTH_LONG).show();
                            }else{
                                apAmount.setText(numStr);
                            }
                            Log.i("金额",numStr);
                        }else if(description.equals("收费日期")){
                            Log.i("收费日期",value);
                            String numStr = "";
                            int k=0;
                            for (int n = 0; n < value.length(); n++) {
                                String subStr = value.substring(n, n+1);
                                Pattern pattern = Pattern.compile("[0-9]+");
                                Matcher matcher = pattern.matcher(subStr);
                                if (matcher.find()) {
                                    numStr +=subStr;
                                }else {
                                    timei[k]=numStr;
                                    k++;
                                    numStr="";
                                }
                                timei[2]=numStr;
                            }
                           time = timei[0]+"年"+timei[1]+"月"+timei[2]+"日 ";
                            apyear.setText(timei[0]);
                            apmonth.setText(timei[1]);
                            apday.setText(timei[2]);
                            if(time.contains("null")){
                                Log.i("时间不对","不对");
                                Date date = new Date();
                                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
                                String time1 = simpleDateFormat.format(date);
                                apTime.setText(time1);
                                Calendar calendar = Calendar.getInstance();
                                String year = String.valueOf(calendar.get(Calendar.YEAR));
                                String month = String.valueOf(calendar.get(Calendar.MONTH) + 1);
                                String day = String.valueOf(calendar.get(Calendar.DAY_OF_MONTH));
                                apyear.setText(year);
                                apmonth.setText(month);
                                apday.setText(day);
                            }
                        }else if(description.equals("收费时间")){
                            Log.i("收费时间",value);
                            String numStr = "";
                            int k=3;
                            for (int n = 0; n < value.length(); n++) {
                                String subStr = value.substring(n, n+1);
                                Pattern pattern = Pattern.compile("[0-9]+");
                                Matcher matcher = pattern.matcher(subStr);
                                if (matcher.find()) {
                                    numStr +=subStr;
                                }else {
                                    timei[k]=numStr;
                                    k++;
                                    numStr="";
                                }
                            }
                            time += timei[3]+":"+timei[4] ;
                            Log.i("time",time);
                            apTime.setText(time);
                        }else if(description.equals("发票所在地")){
                            s=value+" ";
                            apRemarks.setText(s);
                            Log.i("发票所在地",value);
                        }else if(description.equals("车辆通行费代码")){
                            s+=value;
                            apRemarks.setText(s);
                            Log.i("车辆通行费代码",value);
                        }else {
                            Log.i("问题","都不对");
                        }
                    }

                    //抛出错误
                }catch(JSONException e){
                    e.printStackTrace();
                }
                break;
            case "PassengerTicket":
                try{
                    Log.i("jsonarray", String.valueOf(jsonarray));
                    Classification.setSelection(1);
                    String[] timei = new String[5];
                    String time = "";
                    String s="";
                    for(int i = 0 ;i<jsonarray.length();i++)
                    {
                        //通过JSONObject类把JSON字符串转换为java对象，以便于后续操作
                        JSONObject jsonobject = jsonarray.getJSONObject(i);
                        String description = jsonobject.optString("description");
                        String value = jsonobject.optString("value");
                        if (description.equals("金额")){
                            String numStr="";
                            for (int n = 0; n < value.length(); n++) {
                                String subStr = value.substring(n, n+1);
                                Pattern pattern = Pattern.compile("[\\d.]");
                                Matcher matcher = pattern.matcher(subStr);
                                if (matcher.find()) {
                                    numStr +=subStr;
                                }
                            }
                            if(numStr==""){
                                Toast.makeText(getApplicationContext(), "金额为空,请添加金额", Toast.LENGTH_LONG).show();
                            }else{
                                apAmount.setText(numStr);
                            }
                            Log.i("金额",numStr);
                        }else if(description.equals("日期")){
                            Log.i("日期",value);
                            String numStr = "";
                            int k=0;
                            for (int n = 0; n < value.length(); n++) {
                                String subStr = value.substring(n, n+1);
                                Pattern pattern = Pattern.compile("[0-9]+");
                                Matcher matcher = pattern.matcher(subStr);
                                if (matcher.find()) {
                                    numStr +=subStr;
                                }else {
                                    timei[k]=numStr;
                                    k++;
                                    numStr="";
                                }
                                timei[2]=numStr;
                            }
                            time = timei[0]+"年"+timei[1]+"月"+timei[2]+"日 ";
                            apyear.setText(timei[0]);
                            apmonth.setText(timei[1]);
                            apday.setText(timei[2]);
                            apTime.setText(time);
                            if(time.contains("null")){
                                Log.i("时间不对","不对");
                                Date date = new Date();
                                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
                                String time1 = simpleDateFormat.format(date);
                                apTime.setText(time1);
                                Calendar calendar = Calendar.getInstance();
                                String year = String.valueOf(calendar.get(Calendar.YEAR));
                                String month = String.valueOf(calendar.get(Calendar.MONTH) + 1);
                                String day = String.valueOf(calendar.get(Calendar.DAY_OF_MONTH));
                                apyear.setText(year);
                                apmonth.setText(month);
                                apday.setText(day);
                            }
                        }else if(description.equals("时间")){
                            Log.i("时间",value);
                            String numStr = "";
                            int k=3;
                            for (int n = 0; n < value.length(); n++) {
                                String subStr = value.substring(n, n+1);
                                Pattern pattern = Pattern.compile("[0-9]+");
                                Matcher matcher = pattern.matcher(subStr);
                                if (matcher.find()) {
                                    numStr +=subStr;
                                }else {
                                    timei[k]=numStr;
                                    k++;
                                    numStr="";
                                }
                            }
                            time += timei[3]+":"+timei[4] ;
                            Log.i("time",time);
                            apTime.setText(time);
                        }else if(description.equals("发票号码")){
                            apRemarks.setText(value);
                            Log.i("发票号码",value);
                        }else if(description.equals("发票代码")){
                            apBusinessman.setText(value);
                            Log.i("发票代码",value);
                        }else {
                            Log.i("问题","都不对");
                        }
                    }

                    //抛出错误
                }catch(JSONException e){
                    e.printStackTrace();
                }
                break;
            case "PlaneTicket":
                try{
                    Log.i("jsonarray", String.valueOf(jsonarray));
                    Classification.setSelection(1);
                    String[] timei = new String[5];
                    String time = "";
                    String s="";
                    for(int i = 0 ;i<jsonarray.length();i++)
                    {
                        //通过JSONObject类把JSON字符串转换为java对象，以便于后续操作
                        JSONObject jsonobject = jsonarray.getJSONObject(i);
                        String description = jsonobject.optString("description");
                        String value = jsonobject.optString("value");
                        if (description.equals("合计金额")){
                            String numStr="";
                            for (int n = 0; n < value.length(); n++) {
                                String subStr = value.substring(n, n+1);
                                Pattern pattern = Pattern.compile("[\\d.]");
                                Matcher matcher = pattern.matcher(subStr);
                                if (matcher.find()) {
                                    numStr +=subStr;
                                }
                            }
                            if(numStr==""){
                                Toast.makeText(getApplicationContext(), "金额为空,请添加金额", Toast.LENGTH_LONG).show();
                            }else{
                                apAmount.setText(numStr);
                            }
                            Log.i("金额",numStr);
                        }else if(description.equals("填开日期")){
                            Log.i("填开日期",value);
                            String numStr = "";
                            int k=0;
                            for (int n = 0; n < value.length(); n++) {
                                String subStr = value.substring(n, n+1);
                                Pattern pattern = Pattern.compile("[0-9]+");
                                Matcher matcher = pattern.matcher(subStr);
                                if (matcher.find()) {
                                    numStr +=subStr;
                                }else {
                                    timei[k]=numStr;
                                    k++;
                                    numStr="";
                                }
                                timei[2]=numStr;
                            }
                            time = timei[0]+"年"+timei[1]+"月"+timei[2]+"日 ";
                            apyear.setText(timei[0]);
                            apmonth.setText(timei[1]);
                            apday.setText(timei[2]);
                            apTime.setText(time);
                            if(time.contains("null")){
                                Log.i("时间不对","不对");
                                Date date = new Date();
                                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
                                String time1 = simpleDateFormat.format(date);
                                apTime.setText(time1);
                                Calendar calendar = Calendar.getInstance();
                                String year = String.valueOf(calendar.get(Calendar.YEAR));
                                String month = String.valueOf(calendar.get(Calendar.MONTH) + 1);
                                String day = String.valueOf(calendar.get(Calendar.DAY_OF_MONTH));
                                apyear.setText(year);
                                apmonth.setText(month);
                                apday.setText(day);
                            }
                        }else if(description.equals("电子客票号码")){
                            apRemarks.setText(value);
                            Log.i("电子客票号码",value);
                        }else if(description.equals("填开单位")){
                            apBusinessman.setText(value);
                            Log.i("填开单位",value);
                        }else {
                            Log.i("问题","都不对");
                        }
                    }

                    //抛出错误
                }catch(JSONException e){
                    e.printStackTrace();
                }
                break;
        }

    }

        //新增
    public void addphotoconsum(View v){


        consumptionBean = new ConsumptionBean();
        consumptionBean.setConsumption_method(apMethod);
        consumptionBean.setConsumption_class_name(apClassification);
        consumptionBean.setConsumption_money(Float.parseFloat(apAmount.getText().toString()));
        consumptionBean.setConsumption_remarks(apRemarks.getText().toString());
        consumptionBean.setConsumption_business(apBusinessman.getText().toString());
        consumptionBean.setConsumption_time(apTime.getText().toString());
        consumptionBean.setaccount_id(DBManager.selectAccountId()+"");
        consumptionBean.setConsumption_year(Integer.parseInt(apyear.getText().toString()));
        consumptionBean.setConsumption_month(Integer.parseInt(apmonth.getText().toString()));
        consumptionBean.setConsumption_day(Integer.parseInt(apday.getText().toString()));
        DBManager.insertConsumption(consumptionBean);
        DBManager.updateMethodOutmonry(Float.parseFloat(apAmount.getText().toString()),id);
        Log.i("问题","新增？");
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
    }
}